
from jsonschema import validate
from sanic import Blueprint, Request,json as jsonResp
from app.core.entity import ApiResult
from app.service.projectMessage import ProjectMessageService
from app.service.projectUser import ProjectUserService
from app.utils.RequestUtils import RequestUtils


bp=Blueprint("projectMessage",url_prefix="/project/message")

@bp.route("/read/<messageId>",methods=["GET"])
async def readMessage(request:Request,messageId:int):
    message=ProjectMessageService.readMessage(messageId,RequestUtils().getCurrentUserId())
    messageJson=message.toJson()
    messageJson['sender_desc']=message.getSenderDescAttr()
    messageJson['receiver_desc']=message.getReceiverDescAttr()
    return jsonResp(vars(ApiResult.success(data=messageJson)))

@bp.route("/readAll",methods=["GET"])
async def readAllMessage(request:Request):
    message=ProjectMessageService.readAllMessage(RequestUtils().getCurrentUserId())
    return jsonResp(vars(ApiResult.success()))

@bp.route("/list",methods=["GET"])
async def listMyMessage(request:Request):
    messageList=ProjectMessageService.listMyMessage(RequestUtils().getCurrentUserId())
    return jsonResp(vars(ApiResult.success(data={"list":messageList})))